﻿using System;
using System.Windows;
using System.Windows.Controls;

using Hetymine.Executioner.Commands;

namespace Hetymine.Executioner.Plugins.Url {
    /// <summary>
    /// Interaction logic for UrlCommandConfig.xaml
    /// </summary>
    public partial class UrlCommandConfig : UserControl, ICommandConfigurator<ICommand> {
        private bool _IsApply = false;
        private UrlCommand _UrlCommand;

        public UrlCommandConfig() {
            InitializeComponent();

            this.cmdName.TextChanged += new TextChangedEventHandler(cmdName_TextChanged);
            this.cmdUrl.TextChanged += new TextChangedEventHandler(cmdUrl_TextChanged);
            this.cmdCopy.Click += new RoutedEventHandler(cmdCopy_Click);
        }

        private string CommandName {
            get {
                if (this._UrlCommand.IsEmpty) {
                    return string.Empty;
                }

                return this._UrlCommand.ToString();
            }
        }

        private void cmdName_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply || this._UrlCommand == null) { return; }

            this._UrlCommand.Name = this.cmdName.Text.Trim();

            this.OnCommandNameChanged(this, new CommandNameEventArgs(this.CommandName));
        }
        private void cmdUrl_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply || this._UrlCommand == null) { return; }

            this._UrlCommand.Url = this.cmdUrl.Text.Trim();

            this.OnCommandNameChanged(this, new CommandNameEventArgs(this.CommandName));
        }
        private void cmdCopy_Click(object sender, RoutedEventArgs e) {
            if (!this._IsApply || this._UrlCommand == null) { return; }

            this._UrlCommand.Copy = this.cmdCopy.IsChecked.Value;

            this.OnCommandNameChanged(this, new CommandNameEventArgs(this.CommandName));
        }

        #region ICommandConfigurator<ICommand> Members
        public void InitializeConfig(ICommand config) {
            this._IsApply = false;

            if (config == null) {
                this._UrlCommand = null;

                this.cmdName.Text = string.Empty;
                this.cmdUrl.Text = string.Empty;
                this.cmdCopy.IsChecked = false;
            }
            else {
                this._UrlCommand = config as UrlCommand;

                this.cmdName.Text = this._UrlCommand.Name;
                this.cmdUrl.Text = this._UrlCommand.Url;
                this.cmdCopy.IsChecked = this._UrlCommand.Copy;
            }

            this._IsApply = true;
        }

        public UserControl ConfigPanel { get { return this; } }

        public event CommandNameEventHandler CommandNameChanged;
        protected virtual void OnCommandNameChanged(object sender, CommandNameEventArgs e) {
            if (this.CommandNameChanged != null) {
                this.CommandNameChanged(sender, e);
            }
        }
        #endregion
    }
}
